/*
This software is contributed by Hong Jun from the Open Source Software
of Chinese Academy of Sciences. 

This software is distributed under the terms of the GNU General Public License.

THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY
APPLICABLE LAW.  EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT
HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY
OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO,
THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
PURPOSE.  THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM
IS WITH YOU.  SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF
ALL NECESSARY SERVICING, REPAIR OR CORRECTION.
*/
package ac.cas.oss;

import java.net.URL;
import java.net.URLConnection;
import java.util.Iterator;
import java.util.List;

import org.jdom.Element;
import org.jdom.Namespace;
import org.json.JSONObject;

import com.sun.syndication.feed.synd.SyndFeed;
import com.sun.syndication.io.SyndFeedInput;
import com.sun.syndication.io.XmlReader;


/**
 *<b>Description:<b> Obtain the weather data from the given website,
 *and then generate the JSON document.
 *
 *@author <a href="mailto:hongjun.bj@gmail.com">Hong Jun</a>
 *@since Nov 29, 2007 1:40:31 PM
 *@version 0.1
 */
public class DataProcess {

	/**
	 * 
	 */
	public DataProcess() {
	}

	/**
	 * @param urlStr
	 * @return
	 * @throws Exception
	 */
	public String getWeatherData(String urlStr) throws Exception {
		URLConnection feedUrl = new URL(urlStr).openConnection();
		feedUrl.setRequestProperty("User-Agent",
				"Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)");
		SyndFeedInput input = new SyndFeedInput();
		SyndFeed feed = input.build(new XmlReader(feedUrl));
		List list = (List) feed.getForeignMarkup();
		Iterator itr = (Iterator) list.iterator();
		JSONObject root = new JSONObject();
		JSONObject jsonObj = null;
		jsonObj = new JSONObject();
		Element ele = (Element) itr.next();
		Namespace ns = ele.getNamespace();
		Element ob = ele.getChild("ob", ns);
		if (ob != null) {
			jsonObj.put("current_condition", ob.getChild("current-condition",
					ns).getText());
			jsonObj.put("icon", ob.getChild("current-condition", ns)
					.getAttributeValue("icon"));
			jsonObj.put("rain_today", ob.getChild("rain-today", ns).getValue());
			jsonObj.put("wind_speed", ob.getChild("wind-speed", ns).getValue());
			jsonObj.put("city_state", ob.getChild("city-state", ns).getText());
			jsonObj.put("temp", ob.getChild("temp", ns).getValue());
			jsonObj.put("temp_high", ob.getChild("temp-high", ns).getValue());
			jsonObj.put("temp_low", ob.getChild("temp-low", ns).getValue());
			jsonObj.put("humidity", ob.getChild("humidity", ns).getValue());
			jsonObj.put("humidity_high", ob.getChild("humidity-high", ns)
					.getValue());
			jsonObj.put("humidity_low", ob.getChild("humidity-low", ns)
					.getValue());
			jsonObj.put("pressure", ob.getChild("pressure", ns).getValue());
			jsonObj.put("pressure_high", ob.getChild("pressure-high", ns)
					.getValue());
			jsonObj.put("pressure_low", ob.getChild("pressure-low", ns)
					.getValue());
		} else {
			System.out.println("aws:ob not exist!");
		}
		root.append("weather", jsonObj);
		return root.toString();
	}

}
